<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

</body>
</html>
<script type="text/javascript">
    // function twoSum(nums, target) {
    // const map = new Map();
    // for (let i = 0; i < nums.length; i++) {
    //     const complement = target - nums[i];
    //     if (map.has(complement)) {
    //         return [map.get(complement), i]; // 找到解
    //     }
    //
    //     map.set(nums[i], i); // 存储当前数字及其索引
    // }
    // return [];
    // }


    function twoSum(nums,target){
        const index_arr = nums.map((num,index)=>{
            return {
                index:index,
                value:num
            };
        })
        index_arr.sort((a,b)=>a.value-b.value)
        let l=0,r=nums.length-1;
        while(l<=r){
            const sum = index_arr[l].value+index_arr[r].value
            if(sum===target) return [index_arr[l].index,index_arr[r].index].sort((a,b)=>a-b)
            else if(sum>target)  r--
            else l++
        }
        return[]
    }
    console.log(twoSum([3,4,2],6));
</script>